//
//  HMHomeReweetView.swift
//  Weibo
//
//  Created by fuyu on 2017/7/26.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
import SnapKit

//  转发微博视图
class HMHomeReweetView: UIView {
    
    //记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?

    //引用viewModel
    var statusViewModel: HMStatusViewModel? {
        
        didSet {
            
            //  给转发微博绑定数据
            contentLabel.text = statusViewModel?.retweetContent
            //  卸载之前的底部约束
            retweetViewBottomConstraint?.deactivate()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  表示有转发微博对应的配图
                //  1. 显示配图
                pictureView.isHidden = false
                //  2. 重新冲击转发微博视图底部约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  给配图绑定数据
                pictureView.picUrls = picUrls
            } else {
                //  表示没有转发微博对应的配图
                //  1. 隐藏配图
                pictureView.isHidden = true
                //  2. 重新冲击转发微博视图底部约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
        }
    }
    
    
    
    
    //MARK: -- 懒加载控件
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(fontSize: 13, textColor: UIColor.lightGray)
        label.numberOfLines = 0
        return label
        
    }()
    
    //配图
    fileprivate lazy var pictureView: HMHomePictureView = {
        
        let collectionView = HMHomePictureView()
        //  配图颜色和转发微博颜色一样
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
        
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white:0.95,alpha:1.0)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
             //  设置默认大小，后续根据图片张数来确定真是的配图大小
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
        }
        //  关键约束
        self.snp.makeConstraints { (make) in
          retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }

    

}
